package com.fang.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.fang.common.dto.result.SysMenuResult;
import com.fang.entity.SysMenu;
import com.fang.entity.SysUser;
import com.fang.mapper.SysMenuMapper;
import com.fang.mapper.SysUserMapper;
import com.fang.service.SysMenuService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fang.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Optional;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Fang Ruichuan
 * @since 2022-08-27
 */
@Service
public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> implements SysMenuService {

    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private SysUserMapper sysUserMapper;

    @Override
    public List<SysMenuResult> getCurrentUserNav() {
        String username = (String) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        SysUser sysUser = sysUserService.getByUsername(username);
        List<Long> menuIds = sysUserMapper.getNavMenuIds(sysUser.getId());
        List<SysMenu> menus = this.listByIds(Optional.ofNullable(menuIds).orElse(new ArrayList<>()));
        // 转树状结构
        List<SysMenu> menuTree = buildMenuTree(menus);
        // 实体转DTO
        return convert(menuTree);
    }

    @Override
    public List<SysMenuResult> tree() {
        // 获取所有菜单信息
        List<SysMenu> sysMenus = this.list(Wrappers.<SysMenu>lambdaQuery().orderByAsc(SysMenu::getOrderNum));
        // 转成树状结构
        return convert(buildMenuTree(sysMenus));
    }

    private List<SysMenuResult> convert(List<SysMenu> menuTree) {
        List<SysMenuResult> results = new LinkedList<>();
        menuTree.stream().forEach(menu -> {
            SysMenuResult result = SysMenuResult.builder()
                    .name(menu.getPerms())
                    .title(menu.getName())
                    .component(menu.getComponent())
                    .icon(menu.getIcon())
                    .path(menu.getPath())
                    .build();
            if (menu.getChildren().size() > 0) {
                // 子节点调用当前节点
                result.setChildren(convert(menu.getChildren()));
            }
            results.add(result);
        });
        return results;
    }

    private List<SysMenu> buildMenuTree(List<SysMenu> menus) {
        List<SysMenu> finalMenus = new LinkedList<>();
        menus.stream().forEach(menu -> {
            menus.stream().forEach(e -> {
                if (e.getId().equals(menu.getParentId())) {
                    menu.getChildren().add(e);
                }
            });
            if (menu.getParentId().equals(0L)) {
                finalMenus.add(menu);
            }
        });
        return finalMenus;
    }
}
